package com.zb.service.impl;
import com.zb.mapper.ProductMapper;
import com.zb.entity.Product;
import org.springframework.stereotype.Service;
import com.zb.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.zb.util.PageUtil;
import java.util.ArrayList;
import java.util.HashMap;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    public Product getProductById(Long id)throws Exception{
        return productMapper.getProductById(id);
    }

    public List<Product>	getProductListByMap(Map<String,Object> param)throws Exception{
        return productMapper.getProductListByMap(param);
    }

    public Integer getProductCountByMap(Map<String,Object> param)throws Exception{
        return productMapper.getProductCountByMap(param);
    }

    public Integer qdtxAddProduct(Product product)throws Exception{
            //product.setCreatedTime(new Date());
            return productMapper.insertProduct(product);
    }

    public Integer qdtxModifyProduct(Product product)throws Exception{
        //product.setUpdatedTime(new Date());
        return productMapper.updateProduct(product);
    }

    public Integer qdtxDeleteProductById(Long id)throws Exception{
        return productMapper.deleteProductById(id);
    }

    public PageUtil<Product> queryProductPageByMap(Map<String,Object> param,Integer index,Integer size)throws Exception{
        param.put("start",(index-1)*size);
        param.put("size", size);

        List<Product> productList = productMapper.getProductListByMap(param);
        Integer count = productMapper.getProductCountByMap(param);

        PageUtil<Product> page = new PageUtil<Product>();
        page.setData(productList);
        page.setIndex(index);
        page.setSize(size);
        page.setCount(count);
        return page;
    }

    public Integer qdtxBatchDeleteProduct(String ids)throws Exception{
        Map<String,List<String>> param=new HashMap<String,List<String>>();
        String[] paramArrays=ids.split(",");
        List<String> idList=new ArrayList<String>();
            for (String temp:paramArrays){
                idList.add(temp);
            }
        param.put("ids",idList);
        return productMapper.batchDeleteProduct(param);
    }


}
